home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / Date.java < prev    next >
Text File  |  1998-09-22  |  4KB  |  156 lines

  1. /*
  2.  * @(#)Date.java    1.6 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.sql;
  16.  
  17. /**
  18.  * <P>This class is a thin wrapper around java.util.Date that allows
  19.  * JDBC to identify this as a SQL DATE value. It adds formatting and
  20.  * parsing operations to support the JDBC escape syntax for date
  21.  * values.
  22.  */
  23. public class Date extends java.util.Date {
  24.  
  25.     /**
  26.      * Construct a Date  
  27.      *
  28.      * @param year year-1900
  29.      * @param month 0 to 11 
  30.      * @param day 1 to 31
  31.      */
  32.     public Date(int year, int month, int day) {
  33.     super(year, month, day);
  34.     }
  35.  
  36.     /**
  37.      * Construct a Date using a milliseconds time value
  38.      *
  39.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  40.      */
  41.     public Date(long date) {
  42.     // If the millisecond date value contains time info, mask it out.
  43.     super(date);
  44.     int year = getYear();
  45.     int month = getMonth();
  46.     int day = getDate();
  47.     super.setTime(0);
  48.     setYear(year);
  49.     setMonth(month);
  50.     setDate(day);
  51.     }
  52.  
  53.     /**
  54.      * Set a Date using a milliseconds time value
  55.      *
  56.      * @param date milliseconds since January 1, 1970, 00:00:00 GMT
  57.      */
  58.     public void setTime(long date) {
  59.     // If the millisecond date value contains time info, mask it out.
  60.     super.setTime(date);
  61.     int year = getYear();
  62.     int month = getMonth();
  63.     int day = getDate();
  64.     super.setTime(0);
  65.     setYear(year);
  66.     setMonth(month);
  67.     setDate(day);
  68.     }
  69.  
  70.     /**
  71.      * Convert a string in JDBC date escape format to a Date value
  72.      *
  73.      * @param s date in format "yyyy-mm-dd"
  74.      * @return corresponding Date
  75.      */
  76.     public static Date valueOf(String s) {
  77.     int year;
  78.     int month;
  79.     int day;
  80.     int firstDash;
  81.     int secondDash;
  82.  
  83.     if (s == null) throw new java.lang.IllegalArgumentException();
  84.  
  85.     firstDash = s.indexOf('-');
  86.     secondDash = s.indexOf('-', firstDash+1);
  87.     if ((firstDash > 0) & (secondDash > 0) & (secondDash < s.length()-1)) {
  88.         year = Integer.parseInt(s.substring(0, firstDash)) - 1900;
  89.         month = Integer.parseInt(s.substring(firstDash+1, secondDash)) - 1;
  90.         day = Integer.parseInt(s.substring(secondDash+1));     
  91.     } else {
  92.         throw new java.lang.IllegalArgumentException();
  93.     }
  94.             
  95.     return new Date(year, month, day);
  96.     }
  97.  
  98.     /**
  99.      * Format a date in JDBC date escape format  
  100.      *
  101.      * @return a String in yyyy-mm-dd format
  102.      */
  103.     public String toString () {
  104.     int year = super.getYear() + 1900;
  105.     int month = super.getMonth() + 1;
  106.     int day = super.getDate();
  107.     String yearString;
  108.     String monthString;
  109.     String dayString;
  110.  
  111.         
  112.     yearString = Integer.toString(year);
  113.  
  114.     if (month < 10) {
  115.         monthString = "0" + month;
  116.     } else {        
  117.         monthString = Integer.toString(month);
  118.     }
  119.  
  120.     if (day < 10) {
  121.         dayString = "0" + day;
  122.     } else {        
  123.         dayString = Integer.toString(day);
  124.     }
  125.  
  126.     return ( yearString + "-" + monthString + "-" + dayString);
  127.     }
  128.  
  129.     // Override all the time operations inherited from java.util.Date;
  130.     public int getHours() {
  131.     throw new java.lang.IllegalArgumentException();
  132.     }
  133.  
  134.     public int getMinutes() {
  135.     throw new java.lang.IllegalArgumentException();
  136.     }
  137.     
  138.     public int getSeconds() {
  139.     throw new java.lang.IllegalArgumentException();
  140.     }
  141.  
  142.     public void setHours(int i) {
  143.     throw new java.lang.IllegalArgumentException();
  144.     }
  145.  
  146.     public void setMinutes(int i) {
  147.     throw new java.lang.IllegalArgumentException();
  148.     }
  149.  
  150.     public void setSeconds(int i) {
  151.     throw new java.lang.IllegalArgumentException();
  152.     }
  153.  
  154. }
  155.  
  156.